<?php
/**
 */
// from 0 local 1 from_mblog

define("CACHE_PREFIX_PUBU","_pubu_content");

class db_mblog_want_content extends db_base {
    
	var $table_name = "mblog_want_content";
	
	function db_mblog_want_content()
	{
		parent::db_base();
	}
	
	function get_mblog_want_list_by_topic($p=1,$pcount=10,$topic_id="",$sex="",$verified_type="",$online_status="",$uid="")
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-{$p}-{$pcount}-{$topic_id}-{$sex}-{$verified_type}-{$online_status}-{$uid}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$p = intval($p);
			$pcount = intval($pcount);
			
			$sex = mysql_escape_string($sex);
			$topic_id = intval($topic_id);
			
			$online_status = intval($online_status);
			$uid = intval($uid);
			$verified_type = intval($verified_type);
			
			
			$where_ary = array();
			if($sex)$where_ary[] = "sex = $sex";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			if($online_status)$where_ary[] = "online_status = $online_status";
			if($uid)$where_ary[] = "create_uid = $uid";
			if($verified_type)
			{
				$where_ary[] = "verified_type = $verified_type  ";
			}
			else
			{
				//$where_ary[] = " verified_type != -1 ";
				
			}
			$where_ary[] = "status = 1";
			
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,$p,$pcount," create_time desc",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_SEC_10);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	function get_create_uid_by_topic($pcount=10,$topic_id="",$sex="",$verified_type="",$online_status="",$uid="")
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_PUBU."-get_create_uid_by_topic-{$pcount}-{$topic_id}-{$sex}-{$verified_type}-{$online_status}-{$uid}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			
			$pcount = intval($pcount);
			
			$sex = mysql_escape_string($sex);
			$topic_id = intval($topic_id);
			
			$online_status = intval($online_status);
			$uid = intval($uid);
			$verified_type = intval($verified_type);
			
			$where_ary = array();
			if($sex)$where_ary[] = "sex = $sex";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			if($online_status)$where_ary[] = "online_status = $online_status";
			if($uid)$where_ary[] = "create_uid = $uid";
			if($verified_type)
			{
				$where_ary[] = "verified_type = $verified_type  ";
			}
			else
			{
				//$where_ary[] = " verified_type != -1 ";
				
			}
			$where_ary[] = "status = 1";
			
			$where = join (" and ",$where_ary);
			
			$sql = "select distinct(create_uid) as create_uid ,online_status  from {$this->table_name} where $where order by create_time desc limit $pcount";
			$content = $this->select($sql);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_SEC_10);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	
	
	function get_mblog_want_count_by_topic($topic_id="",$sex="",$verified_type="",$online_status="",$uid="")
	{
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_PUBU."-get_mblog_want_count_by_topic-{$topic_id}-{$sex}-{$verified_type}-{$online_status}-{$uid}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			
			
			$sex = mysql_escape_string($sex);
			$topic_id = intval($topic_id);
			
			$online_status = intval($online_status);
			$uid = intval($uid);
			$verified_type = intval($verified_type);
			
			
			$where_ary = array();
			if($sex)$where_ary[] = "sex = $sex";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			if($online_status)$where_ary[] = "online_status = $online_status";
			if($uid)$where_ary[] = "create_uid = $uid";
			if($verified_type)
			{
				$where_ary[] = "verified_type = $verified_type  ";
			}
			else
			{
				//$where_ary[] = " verified_type != -1 ";
				
			}
			$where_ary[] = "status = 1";
			
			$where = join (" and ",$where_ary);
			
			$content = $this->get_listcount($this->table_name,$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_SEC_10);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	function insert_mblog_want_content($content,$topic_id,$topic_get,$topic,$pic,$pic_width_pubu,$pic_height_pubu,$source,$show_type,$from,
	$uid,$nickname,$update_date,$mid,$create_time,$count_love,$sex,$verified_type)
	{
		
		if(!$mid)
		{
			return false;
		}
		//内容先转码
		$content = html_entity_decode($content,ENT_QUOTES);
		//删除@信息
		$content= preg_replace("/<.*?>/u","",$content);
		
		
		$ary["count_love"] = $count_love;
		$ary["content"] = $content;
		
		$ary["topic_id"] = $topic_id;
		$ary["topic"] = $topic;
		$ary["topic_get"] = $topic_get;
		$ary["pic"] = $pic;
		$ary["pic_width_pubu"] = $pic_width_pubu;
		$ary["pic_height_pubu"] = $pic_height_pubu;
		$ary["source"] = $source;
		$ary["show_type"] = $show_type;
		$ary["from"] = $from;
		$ary["mid"] = $mid;
		if($sex == "m")
		{
			$sex = 1;
		}
		else
		{
			$sex = 0;
		}
		$ary["sex"] = $sex;
		$ary["verified_type"] = $verified_type;
		$ary["status"] = 1;
		$ary["create_uid"] = $uid;
		$ary["create_nickname"] = $nickname;
		$ary["create_time"] = $create_time;
		$ary["create_ip"] = 0;
		
		$update_date = strtotime($create_time) + 86400;
		$ary["update_date"] = $update_date;
		
		
		$ret = $this->insert($this->table_name, $ary );
		
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_count_by_topic-{$topic_id}-0-0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_count_by_topic-{$topic_id}--0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_count_by_topic-0-0-0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_count_by_topic-0--0";
		
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-{$topic_id}-0-0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-{$topic_id}--0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-0--0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-0-0-0";
		$mc = mem_cache::get_instance();
		$cache = $mc->delete_ary($key);
		
		return $this->get_lastinsertid();
		
		
	}
	
	function update_online_by_uid($create_uid,$online_status)
	{
		$create_uid = intval($create_uid);
		
		$where = "create_uid = {$create_uid}";
		$ary["online_status"] = $online_status;
		
		$res = $this->update($this->table_name,$ary,$where);
		return $res;
	}
	
	function update_by_mid($mid,$count_reply,$id)
	{
		$ary=array();
		if($count_reply)
		{
			$ary["count_reply"] = "count_reply+{$count_reply}";
		}
		$mid = intval($mid);
		
		$where = "mid = $mid";
		
		$res = $this->update($this->table_name,$ary,$where);
		
		$item = $this->get_one_by_id($id);
		$topic_id = $item["topic_id"];
		
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-{$topic_id}-0-0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-{$topic_id}--0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-0--0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-0-0-0";
		$mc = mem_cache::get_instance();
		$cache = $mc->delete_ary($key);
		
		
		return $res;
	}
	
	
	function update_count_reply_by_id($id,$count_reply)
	{
		$ary=array();
		if($count_reply)
		{
			$ary["count_reply"] = "count_reply+{$count_reply}";
		}
		$ary["have_att"] = 1;
		
		$mid = intval($mid);
		
		$where = "id = $id";
		
		$res = $this->update($this->table_name,$ary,$where);
		
		$item = $this->get_one_by_id($id);
		$topic_id = $item["topic_id"];
		
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-{$topic_id}-0-0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-{$topic_id}--0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-0--0";
		$key[] = CACHE_PREFIX_PUBU."-get_mblog_want_list_by_topic-1-20-0-0-0";
		$mc = mem_cache::get_instance();
		$cache = $mc->delete_ary($key);
		
		
		return $res;
	}
	
	
	function get_one_by_id($id)
	{
		$id = intval($id);
		
		$where = "id = $id";
		$data = $this->get_alllist($this->table_name,1,1,"",$where);
		return $data[0];
	}
	
###########################

	
		/**
	 * 用户注册
	 * @param $user_name
	 * @param $password
	 */
	function admin_insert_mblog_want_content_by_cron_id($id)
	{
		// 插入数据
		$ary = array();
		$from = "topic_mblog";
		
		
		
		$sql ="insert ignore into {$this->table_name} (show_type,`from`,mid,content,md5_content,topic_id,topic,pic,pic_width_pubu,pic_height_pubu,split_word,source,location,coordinates,create_uid,create_nickname,create_time,create_ip,status,count_love) select show_type,'1',id,content,md5(content),topic_id,topic,pic,pic_width_pubu,pic_height_pubu,split_word,source,location,coordinates,create_uid,create_nickname,create_time,create_ip,status,count_love from mblog_cron_content where id =$id";
		
		$ret = $this->query($sql);
	
		return $ret;
	}
	
	
	/**
	 * cron 获取food_tag
	 */
	function admin_get_count_by_search($status="",$min_height="",$max_height="",$topic="",$show_type="")
	{
		$where= array();
		if(strlen($status) > 0)
		{
			$status = intval($status);
			$where[] = "status = {$status}";
		}
		
		if($min_height) 
		{
			$where[] = "pic_height_pubu > '{$min_height}'";
		}
		if($max_height)
		{
			$where[] = "pic_height_pubu < '{$max_height}'";
		}
		if($topic)
		{
			$where[] = "topic = '{$topic}'";
		}
		if($show_type)
		{
			$where[] = "show_type = '{$show_type}'";
		}
		
		$where= join(" and ",$where);
		
		$data = $this->get_listcount($this->table_name, $where);
		
		return $data;
	}
	
	
	/*
	 * cron获取topic_lisy
	 */
	function admin_get_list_by_search($status="",$p=1,$pcount=10,$min_height="",$max_height="",$topic="",$show_type="")
	{
		$where= array();
		if(strlen($status) > 0)
		{
			$status = intval($status);
			$where[] = "status = {$status}";
		}
		if($min_height) 
		{
			$where[] = "pic_height_pubu > '{$min_height}'";
		}
		if($max_height)
		{
			$where[] = "pic_height_pubu < '{$max_height}'";
		}
		if($topic)
		{
			$where[] = "topic = '{$topic}'";
		}
	if($show_type)
		{
			$where[] = "show_type = '{$show_type}'";
		}
		$where= join(" and ",$where);
		
		$p = intval($p);
		$pcount = intval($pcount);
		
		
		$data = $this->get_alllist($this->table_name,$p,$pcount,"create_time desc",$where);
		
		return $data;
	}
	
		
	function admin_update_mblog_info_by_id($id,$status="")
	{
		$where = "id = $id";
		$ary = array();

		if(strlen($status)>0)$ary["status"] = $status;
		$ret = $this->update($this->table_name, $ary,$where);
		
		return $ret;
	}
	
	
function cron_get_data_list_width_wrong($p,$pcount)
	{
		
		$where = "pic_height_pubu = 0 and pic!='' and create_ip != -1";
		$data = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
		return $data;
	}
	
	
#######################	
	function cron_get_data_count_width_wrong()
	{
		
		$where = "pic_height_pubu = 0 and pic!='' and create_ip != -1";
		$data = $this->get_listcount($this->table_name,$where);
		return $data;
	}
	
	function cron_update_mblog_info_by_id($id,$pic_width_pubu="",$pic_height_pubu="",$status="")
	{
		$where = "id = $id";
		$ary = array();

		if(strlen($pic_width_pubu)>0)$ary["pic_width_pubu"] = $pic_width_pubu;
		if(strlen($pic_height_pubu)>0)$ary["pic_height_pubu"] = $pic_height_pubu;
		if(strlen($status)>0)$ary["status"] = $status;
		$ret = $this->update($this->table_name, $ary,$where);
		
		return $ret;
	}
	
/*  	function get_user_order_by_count_tuijian_nocache($show_type,$p,$pcount)
	{
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$topic_list = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_list_order_by_like($show_type,1,10);
		
		
		$content = array();
		foreach($topic_list as $value)
		{
			$topic_id = $value["id"];
			$sql ="select * from mblog_want_content where topic_id = $topic_id order by  update_date desc , count_tuijian desc limit 1";
			
			$res = $db_mblog_cron_topic_tag->select($sql);
			
			$res = $res[0];
			$content[] = $res;
		}
		
		return $content ;
	}*/
	
	
	
	function cron_insert_mblog_want_content($content,$topic_id,$topic_get,$topic,$pic,$pic_width_pubu,$pic_height_pubu,$source,$show_type,$from,
	$uid,$nickname,$update_date,$mid,$create_time,$count_love,$sex,$verified_type,$mid_62,$head_url)
	{
		//内容先转码
		$content = html_entity_decode($content,ENT_QUOTES);
		//删除@信息
		$content= preg_replace("/<.*?>/u","",$content);
		
		
		$ary["count_love"] = $count_love;
		$ary["content"] = $content;
		
		$ary["topic_id"] = $topic_id;
		$ary["topic"] = $topic;
		$ary["topic_get"] = $topic_get;
		$ary["pic"] = $pic;
		$ary["pic_width_pubu"] = $pic_width_pubu;
		$ary["pic_height_pubu"] = $pic_height_pubu;
		$ary["source"] = $source;
		$ary["show_type"] = $show_type;
		$ary["from"] = $from;
		$ary["mid"] = $mid;
		$ary["mid_62"] = $mid_62;
		$ary["head_url"] = $head_url;
		
		
		
		$ary["sex"] = $sex;
		$ary["verified_type"] = $verified_type;
		$ary["status"] = 1;
		$ary["create_uid"] = $uid;
		$ary["create_nickname"] = $nickname;
		$ary["create_time"] = $create_time;
		$ary["create_ip"] = 0;
		
		$update_date = strtotime($create_time);
		if($count_love)$update_date = log($count_love)*86400 + $update_date;
		$ary["update_date"] = $update_date;
		
		
		$ret = $this->insert($this->table_name, $ary );
		if($ret)
		{
			return $this->get_lastinsertid();
		}
		else
		{
			return false;
		}
		
		
	}
	
	
	
	//抓取用户最近的微博
	function cron_get_mblog_list_order_by_create_time($uid,$p=1,$pcount=10)
	{
		$where = "create_uid = {$uid} and status = 1";
		$list = $this->get_alllist($this->table_name,$p,$pcount,"",$where)	;
		return $list;
	}
	
	
	
	function cron_get_min_time_by_topic($topic_id)
	{
		$sql ="select min(create_time) as create_time from mblog_want_content where topic_id =  $topic_id and create_ip = 0";
		
		$res = $this->select($sql);
	
		return $res;
	}
	
	
	function cron_get_max_time_by_topic_get($topic_get)
	{
	
		$sql = "select max(create_time) as create_time from mblog_want_content where topic_get = '{$topic_get}' limit 0,1";
		$res= $this->select($sql);
		return $res[0];
		
		
	
	}
	
	
	function cron_get_info_by_tipoc_id($topic_id,$sex)
	{
		
		if($sex)
		{
			$sql = "select create_uid from mblog_want_content where topic_id = {$topic_id} and sex={$sex} group by create_uid order by create_time desc limit 20";
		
		}
		else
		{
			$sql = "select create_uid from mblog_want_content where topic_id = {$topic_id} group by create_uid order by create_time desc limit 20";
		
		}
		
		$res= $this->select($sql);
		return $res;
		
		
		
	
	}
	
	
	
	
	function cron_get_list_for_reply_from_sina($p,$pcount,$id)
	{
		$where = "id > $id and status = 1 ";
		$data = $this->get_alllist($this->table_name,$p,$pcount,"id",$where);
	
		return $data;
	}
	
	function cron_get_list_for_reply_have_att($p,$pcount)
	{
		$where = "have_att=1 and status = 1 ";
		$data = $this->get_alllist($this->table_name,$p,$pcount,"create_time desc",$where,"mid,from,id");
	
		return $data;
	}
	
	function cron_update_mblog_count_reply($id)
	{
		$db_mblog_want_comment = new db_mblog_want_comment();
		
		$count = $db_mblog_want_comment->cron_get_count_by_mid($id);
		
		$sql = "update {$this->table_name} set count_reply = $count where id = $id ";
		$this->query($sql);
		
		
	}
	
	
}



